//
//  HYWebSiteHeaderView.m
//  XPlayer
//
//  Created by liguoqing on 2023/10/27.
//

#import "HYWebSiteHeaderView.h"
#import "HYInternetViewModel.h"
#import "HYWebSiteViewCell.h"

@interface HYWebSiteHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) HYInternetViewModel *viewModel;

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation HYWebSiteHeaderView

- (instancetype)initWithViewModel:(HYInternetViewModel *)viewModel
{
    self = [super init];
    if (self) {
        self.backgroundColor = AppContentBgThemeColor;
        self.viewModel = viewModel;
        [self configUI];
        [self bindSignal];
    }
    return self;
}

- (void)configUI
{
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.updatedContentSignal subscribeNext:^(id x) {
        @strongify(self);
        [self.collectionView reloadData];
    }];
}

#pragma mark - UICollectionViewDelegate && UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.viewModel numberOfItemsIsHeader:YES];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HYWebSiteViewCell *collectCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HYWebSiteViewCell" forIndexPath:indexPath];;
    [collectCell bindViewModel:[self.viewModel cellForRowAtIndexPath:indexPath isHeader:YES]];
    return collectCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.viewModel didSelectItemAtIndexPath:indexPath isHeader:YES];
}

- (void)reloadCollect:(NSIndexPath *)indexPath
{
    if(indexPath){
        [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
    }else{
        [self.collectionView reloadData];
    }
}

#pragma mark - loadLazy
- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(kItemWidth(4), kItemHeight(4));
        layout.minimumLineSpacing = kItemSpace;
        layout.minimumInteritemSpacing = kItemSpace;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = UIColor.clearColor;
        _collectionView.contentInset = UIEdgeInsetsMake(kItemSpace, kItemSpace, kItemSpace, kItemSpace);
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[HYWebSiteViewCell class] forCellWithReuseIdentifier:@"HYWebSiteViewCell"];
    }
    return _collectionView;
}

@end
